﻿<!doctype html>
<html lang="en">
	<head>
		<title>JavaScript Patterns</title>
		<meta charset="utf-8">
	</head>
	<body>
		<script>
			/* Title: Command
			 Description: creates objects which encapsulate actions and parameters
			 */

			(function () {

				var CarManager = {

					/* request information */
					requestInfo:function (model, id) {
						return 'The purchase info for ' + model + ' with ID ' + id + ' is being processed...';
					},

					/* purchase the car */
					buyVehicle:function (model, id) {
						return 'You have successfully purchased Item ' + id + ', a ' + model + '.';
					}

				};

				CarManager.execute = function (commad) {
					return CarManager[commad.request](commad.model, commad.carID);
				};

				var actionA = CarManager.execute({request:'requestInfo', model:'Ford Mondeo', carID:'543434'});
				console.log(actionA);
				var actionB = CarManager.execute({request:'buyVehicle', model:'Ford Mondeo', carID:'543434'});
				console.log(actionB);

			})();

			// reference
			// http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#commandpatternjavascript
		</script>
	</body>
</html>